using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

using System.Data;
using System.Data.Common;
using System.Data.SqlClient;

using PickGold;
using PickGold.Database;
using PickGold.Entities.Table;

namespace PickGold.Base
{
    public class CMessages : CBaseMimeInfo, IMessages
    {
        //
        private SMessages _ = null;
        //
        public CMessages() : base() { }
        public CMessages(CDatabase db) : base(db) { }
        public CMessages(string alias) : base(alias) { }
        public CMessages(CDatabase db, string alias) : base(db, alias) { }
        //
        protected override void SetSchema()
        {
            this.SetSchema(new SMessages());
        }
        protected void SetSchema(SMessages s)
        {
            this._ = s;
            base.SetSchema(s);
        }
        //
        public string this[long index]
        {
            get
            {
                return this.getValue(index);
            }
        }
        public string this[string name]
        {
            get
            {
                return this.getValue(name);
            }
        }
        //
        public XmlNode getNode(long index)
        {
            return this.GetXml().SelectSingleNode(string.Format("//*[@{0}={1}]", ITEM_ID, index));
        }
        public XmlNode getNode(string name)
        {
            return this.GetXml().SelectSingleNode(string.Format("//*[@{0}='{1}']", ITEM_NAME, name));
        }
        //
        public override long GetID(string name, long super)
        {
            if (name[0] == '.')
                name = name.Substring(1);
            if (super > 0)
                return base.GetID(name, super);
            XmlNode node = this.GetXml().SelectSingleNode(string.Format("//*[@{0}='{1}']/@{2}", ITEM_NAME, name, ITEM_ID));
            if (node == null)
                return base.GetID(name, super);
            return long.Parse(node.Value);
        }
        //
        public override string GetName(long id, string separator, bool reversal)
        {
            if (!string.IsNullOrEmpty(separator))
                return base.GetName(id, separator, reversal);
            XmlNode node = this.GetXml().SelectSingleNode(string.Format("//*[@{0}={1}]/@{2}", ITEM_ID, id, ITEM_NAME));
            if (node == null)
                return base.GetName(id, separator, reversal);
            return node.Value;
        }
        //
        public string getValue(long index)
        {
            XmlNode node = this.getNode(index);
            if (node == null)
            {
                if (this.Initialize(index) > 0)
                    node = this.getNode(index);
                if (node == null)
                    return string.Empty;
            }
            node = node.Attributes.GetNamedItem(ITEM_VALUE);
            if (node == null)
                return string.Empty;
            return node.Value;
        }
        public string getValue(string name)
        {
            XmlNode node = this.getNode(name);
            if (node == null)
            {
                if (this.Initialize(name) > 0)
                    node = this.getNode(name);
                if (node == null)
                    return string.Empty;
            }
            node = node.Attributes.GetNamedItem(ITEM_VALUE);
            if (node == null)
                return string.Empty;
            return node.Value;
        }
        //
        public string Format(object arg)
        {
            return string.Format(this._.ItemValue, arg);
        }
        public string Format(object arg0, object arg1)
        {
            return string.Format(this._.ItemValue, arg0, arg1);
        }
        public string Format(object arg0, object arg1, object arg2)
        {
            return string.Format(this._.ItemValue, arg0, arg1, arg2);
        }
        public string Format(object[] args)
        {
            return string.Format(this._.ItemValue, args);
        }
        //
    }
    //
    public class SMessages : SBaseMimeInfo
    {
    }
    //
}
//
